﻿//查询对象
function queryObj (serverUrl,layerName,where, geometryType, coordArr, gdbVersion, inSR, outSR) {
    var queryUrl = "";
    var result = "";
    var strGeometry = "";
    var query = new Object;
    query.serverUrl = serverUrl;
    query.layerName = layerName;
    query.where = where;
    query.geometryType = geometryType;//point | polyline | polygon
    query.coordArr = coordArr;//[[000,000],[111,111],[222,222]]  ||  [[000,000,000],[111,111,111],[222,222,222]]
    query.gdbVersion = gdbVersion;
    query.inSR = inSR;//  4326  |  3857
    query.outSR = outSR;
    //geoserver查询方法
    query.queryGeoserver = function () {
        //queryUrl = serverUrl + "/ows?service=wfs&" +
        //"version=" + this.gdbVersion + "&request=DescribeFeatureType&typeName=" + this.layerName + "&" +
        //"outputFormat=application/json";
        var filter = "";
        if (this.geometryType=="point") {

        }
        else if (this.geometryType=="polyline") {
            filter = "<Filter xmlns='http://www.opengis.net/ogc' xmlns:gml='http://www.opengis.net/gml'>  <Intersects><PropertyName>the_geom</PropertyName><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>" + strGeometry + "</gml:coordinates> </gml:LinearRing> </gml:outerBoundaryIs> </gml:Polygon></Intersects></Filter>";
        }
        else if (this.geometryType=="polygon") {
            filter = "<Filter xmlns='http://www.opengis.net/ogc' xmlns:gml='http://www.opengis.net/gml'>  <Intersects><PropertyName>the_geom</PropertyName><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>" + strGeometry + "</gml:coordinates> </gml:LinearRing> </gml:outerBoundaryIs> </gml:Polygon></Intersects></Filter>";
        }
        else {
            filter = "";
        }
        queryUrl = geoserverBaseUrl + "/ows?service=WFS&version=" + this.gdbVersion + "&request=GetFeature&typeName=" + this.layerName + "&maxFeatures=500&outputFormat=application/json" + "&filter=" + filter;
        var qdata = "";
        $.ajaxSettings.async = false;
        $.getJSON(queryUrl, function (data) {
            qdata = data;
            return qdata;
        });
        $.ajaxSettings.async = true;
        return qdata;
    }

    //arcgis查询方法
    query.queryArcgis = function () {
        if (this.geometryType == "point") {
            if (this.coordArr.length==1) {
                this.geometryType = "esriGeometryPoint";//单点
                if (this.coordArr[0].length==2) {
                    strGeometry = "{'x':" + this.coordArr[0][0] + ",'y':" + this.coordArr[0][1] + "}";
                    }
                else if (this.coordArr[0].length==3) {
                    strGeometry = "{'x':" + this.coordArr[0][0] + ",'y':" + this.coordArr[0][1] + ",'z':" + this.coordArr[0][2] + "}";
                }
            }
            else {
                this.geometryType = "esriGeometryMultipoint";//多点
                strGeometry = getStrGeometry(coordArr);
                strGeometry = "{'points':[" + strGeometry + "]}";
            }
        }
        else if (this, geometryType == "polyline") {//线
            this.geometryType = "esriGeometryPolyline";
            strGeometry = getStrGeometry(coordArr);
            strGeometry = "{'paths':[[" + strGeometry + "],[]]}";

        }
        else if (this.geometryType == "polygon") {//面
            this.geometryType = "esriGeometryPolygon";
            strGeometry = getStrGeometry(coordArr);
            strGeometry = "{'rings':[[" + strGeometry + "],[]]}";
        }

        queryUrl = this.serverUrl + "/"+this.layerName+"/query?where=" + this.where + "&text=&objectIds=&time=&geometry=" + strGeometry+ "&geometryType=" + this.geometryType + "&inSR=" + this.inSR + "&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=" + this.outSR + "&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=" + this.gdbVersion + "&returnDistinctValues=false&f=pjson";
        
        $.ajaxSettings.async = false;
        $.getJSON(queryUrl, function (data) {
            result = data;
        });
        $.ajaxSettings.async = true;
        return result;
    }
    return query;
}

//获取坐标字符串
function getStrGeometry(coordArr) {
    strGeometry = "";
    for (var i = 0; i < coordArr.length; i++) {
        if (i == 0) {
            strGeometry += "[" + coordArr[i] + "]";
        }
        else {
            strGeometry += ",[" + coordArr[i] + "]";
        }
    }
    return strGeometry;
}


var query = new queryObj("http://localhost:8080/geoserver/wfs", "xzscmcxq:XZQ", "1=1", "polygon", [[123, 456], [789, 123], [789, 456], [123, 456]], "", "", "");
var str = query.queryGeoserver();
console.log(str);